//
//  DataManager.m
//  OneDay
//
//  Created by lanou3g on 15/11/4.
//  Copyright © 2015年 oneDay. All rights reserved.
//

#import "DataManager.h"
#import "VideoModel.h"

@implementation DataManager

// 创建单例类  进行对数据库 操作方法的封装
+ (DataManager *)shareManager
{
    static DataManager *manager = nil;
    if (manager == nil) {
        manager = [[DataManager alloc] init];
        
        // 获取路径
        NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        NSString *dbPath = [documentsPath stringByAppendingPathComponent:@"OneDay.sqlite"];
        NSLog(@"%@",dbPath);
        // 初始化FMDB
        manager.database = [FMDatabase databaseWithPath:dbPath];
        
        [manager creatTable];
    }
    return manager;
}

// 创建表
- (void)creatTable
{
    [self.database open];
    // executeUpdate:@"create table 表名 (列名 类型,..... )"
    [self.database executeUpdate:@"create table Party (title text, image text, needStr text)"];
    [self.database executeUpdate:@"create table Scroll (title text, image text, needStr text)"];
    [self.database executeUpdate:@"create table Video (title text, image text, needStr text)"];
    [self.database executeUpdate:@"create table Article (title text, image text, needStr text)"];
    NSLog(@"创建表成功");
    [self.database close];
    
}

// 插入
- (void)insertDatabaseWithTitle:(NSString *)title imageUrl:(NSString *)imageUrl needString:(id)needStr num:(NSInteger)num
{
    [self.database open];
    NSString *tempStr;
    if (num == 1) {
        tempStr = @"insert into Party(title,image,needStr) values(?,?,?)";
    }else if (num == 2) {
        tempStr = @"insert into Video(title,image,needStr) values(?,?,?)";
    }else if (num == 3) {
        tempStr = @"insert into Scroll(title,image,needStr) values(?,?,?)";
    }else {
        tempStr = @"insert into Article(title,image,needStr) values(?,?,?)";
    }
    
    FMResultSet *wang = [self.database executeQuery:tempStr withArgumentsInArray:@[title, imageUrl, needStr]];
    [wang next];
    
    NSLog(@"插入数据成功");
    [self.database close];
}

// 删除
- (void)deleteWithTitle:(NSString *)title num:(NSInteger)num
{
    [self.database open];
    //delete from 表名 where 条件
    if (num == 1) {
        [self.database executeUpdateWithFormat:@"delete from Party where title = %@",title];
    }else if (num == 2){
        [self.database executeUpdateWithFormat:@"delete from Video where title = %@",title];
    }else if (num == 3){
        [self.database executeUpdateWithFormat:@"delete from Scroll where title = %@",title];
    }else{
        [self.database executeUpdateWithFormat:@"delete from Article where title = %@",title];
    }
    
//    //删除所有
//    [self.database executeUpdateWithFormat:@"delete from user"];
    
    NSLog(@"删除数据成功");
    [self.database close];
}

// 查询单个
- (NSString *)selectWithNum:(NSInteger)num title:(NSString *)title
{
    [self.database open];
    // 按条件查询 [self.database intForQuery(根据类型来写) :@"select 要返回的属性 from 数据库名 where 查询条件的属性 = ?",@"查询条件"]
    //        NSInteger age = [self.database intForQuery:@"select age from user where name = ?",@"张三"];
    //        NSLog(@"%ld", age);
    NSString *selectTitle;
    if (num == 1) {
        selectTitle = [self.database stringForQuery:@"select needStr from Party where title = ?", title];
    }else if (num == 2){
        selectTitle = [self.database stringForQuery:@"select needStr from Video where title = ?", title];
    }else if (num == 3){
        selectTitle = [self.database stringForQuery:@"select needStr from Scroll where title = ?", title];
    }else{
        selectTitle = [self.database stringForQuery:@"select needStr from Article where title = ?", title];
    }
    [self.database close];
    return selectTitle;
}

// 查询全部
- (NSMutableArray *)selectWithNum:(NSInteger)num
{
    [self.database open];
    
    NSString *str;
    if (num == 1) {
        str = @"select * from Party";
    }else if (num == 2){
        str = @"select * from Video";
    }else if (num == 3){
        str = @"select * from Scroll";
    }else{
        str = @"select * from Article";
    }
    //查询整个表
    FMResultSet * resultSet = [self.database executeQuery:str];
    
    //[resultSet next]遍历条件 遍历前为YES 遍历结束为NO
    NSMutableArray *array = [NSMutableArray array];
    while ([resultSet next] == YES)
    {
        //  用[resultSet stringForColumn:]或[resultSet intForColumn:]等方法把值转成Object-C的型态
        if (num == 1 || num == 2 || num == 3) {
            NSString *title = [resultSet stringForColumn:@"title"];
            NSString *imageUrl = [resultSet stringForColumn:@"image"];
            NSString *needStr = [resultSet stringForColumn:@"needStr"];
            NSArray *tempArray = @[title, imageUrl, needStr];
            [array addObject:tempArray];
        }else{
            NSString *title = [resultSet stringForColumn:@"title"];
            NSString *needStr = [resultSet stringForColumn:@"needStr"];
            NSArray *tempArray = @[title, needStr];
            [array addObject:tempArray];
        }
    }
    [self.database close];
    return array;
}

// 传进来 判断状态
- (void)isLogin:(BOOL)isLogin
{
    NSUserDefaults *isLoginDefaults = [NSUserDefaults standardUserDefaults];
    [isLoginDefaults setBool:isLogin forKey:kLoginState];
    [isLoginDefaults synchronize];
}
- (BOOL)isLogin
{
    return [[NSUserDefaults standardUserDefaults] boolForKey:kLoginState];
}

// 传进来账号 把账号最轻量级持久化
- (void)userName:(NSString *)userName
{
    // 持久化
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:userName forKey:kUserName];
    // 同步数据
    [userDefaults synchronize];
    
}
// 返回账号
- (NSString *)userName
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:kUserName];
}
// 传进来密码  把密码持久化
- (void)userPassword:(NSString *)userPassword
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:userPassword forKey:kPassword];
    [userDefaults synchronize];
}
- (NSString *)userPassword
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:kPassword];
}




@end
